package com.ft.scan.common;

import lombok.Getter;
import org.json.JSONObject;

@Getter
public class TextInResult<T> {
    private int code;
    private String message;
    private T data;

    // 构造器、getter和setter方法省略

    public static <T> TextInResult<T> ok(T data) {
        TextInResult<T> result = new TextInResult<>();
        result.setCode(200);
        result.setMessage("成功");
        result.setData(data);
        return result;
    }


    public static <T> TextInResult<T> fail(int code, String message) {
        TextInResult<T> result = new TextInResult<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    // 解析服务端返回的 JSON 数据并封装成 TextInResult 对象
    public static TextInResult<String> parse(JSONObject jsonResponse) {
        TextInResult<String> result = new TextInResult<>();

        // 根据具体的 JSON 结构解析并封装成 TextInResult 对象
        int code = jsonResponse.getInt("code");
        String message = jsonResponse.getString("message");
        String data = jsonResponse.optString("data", null);

        result.setCode(code);
        result.setMessage(message);
        result.setData(data);

        return result;
    }
}
